package today.rocky.boot.eureka.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import today.rocky.boot.eureka.feign.ProFeign;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Rocky
 * @date 2020/2/25 16:22
 */
@RestController
@CrossOrigin
public class EurekaController {
    @Autowired
    private ProFeign proFeign;

    @Autowired
    private DiscoveryClient discoveryClient;



    @GetMapping("/hello")
    public String sayHello(){
       return proFeign.getHello();
    }

    @GetMapping("/echo/{message}")
//    @HystrixCommand(
//            fallbackMethod = "errorContent", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "100")
//    })
    public String echo(@PathVariable String message) throws Exception {
        return proFeign.echo(message);
    }

    public String errorContent(String message) {
        return "Fault"+message;
    }


    @GetMapping("/services")
    public Set<String> getService(){
        return new LinkedHashSet<>(discoveryClient.getServices());
    }

    @GetMapping("/services/{serviceName}")
    public List<ServiceInstance> getServiceInstance(@PathVariable String serviceName){
        return discoveryClient.getInstances(serviceName);
    }
}
